package cn.cl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.cl.gulimall.product.entity.CategoryEntity;
import cn.cl.gulimall.product.service.CategoryService;
import cn.cl.common.utils.R;


/**
 * 商品三级分类
 *
 * @author 高志红
 * @email 1739745826@qq.com
 * @date 2020-05-07 19:59:25
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
	@Autowired
	private CategoryService categoryService;

	/**
	 * 查出所有分类以及子分类，以树形结构组装起来
	 */
	@RequestMapping("/list/tree")
	public R list() {
		List<CategoryEntity> categoryEntities = categoryService.listWithTree();
		return R.ok().put("data", categoryEntities);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{catId}")
	// @RequiresPermissions("product:category:info")
	public R info(@PathVariable("catId") Long catId) {
		CategoryEntity category = categoryService.getById(catId);

		return R.ok().put("data", category);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody CategoryEntity category) {
		categoryService.save(category);
		return R.ok();
	}

	/**
	 * 拖拽功能的批量修改
	 */
	@RequestMapping("/batchUpdate")
	public R batchUpdate(@RequestBody CategoryEntity[] categorys) {
		categoryService.updateBatchById(Arrays.asList(categorys));
		return R.ok();
	}

	/**
	 * 分类级联更新
	 */
	@RequestMapping("/update")
	public R update(@RequestBody CategoryEntity category) {
		categoryService.updateCascade(category);
		return R.ok();
	}

	/**
	 * 删除
	 * 只要有 @RequestBody  就必须发送post请求
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] catIds) {
		categoryService.removeMenuByIds(Arrays.asList(catIds));
		return R.ok();
	}
}
